# usage_crud.py
from typing import List
from sqlalchemy.orm import Session
from ..models.usage_model import Usage
from datetime import date
from fastapi import HTTPException

def get_usage_by_date(db: Session, data: date) -> List[Usage]:
    result=db.query(Usage).filter(Usage.data == data).all()
    if not result:
        raise HTTPException(status_code=404, detail="No usage found")
    usage_list = [usage.as_dict() for usage in result]
    return usage_list


def create_usage(db: Session, usage: Usage) -> Usage:
    db.add(usage)
    db.commit()
    db.refresh(usage)
    return usage.as_dict()

def get_usage_by_date_range(db: Session, start_date: date, end_date: date) -> List[Usage]:

    result=db.query(Usage).filter(
        Usage.data >= start_date,
        Usage.data <= end_date
    ).all()
    if not result:
        raise HTTPException(status_code=404, detail="No usage found")
    return [usage.as_dict() for usage in result]


def get_usage_by_date_and_user(db: Session, data: date, user_id: int) -> Usage:

    return db.query(Usage).filter(
        Usage.data == data,
        Usage.user_id == user_id
    ).first()
